﻿@model TaxonomyFieldViewModel
@using Orchard.Taxonomies.Helpers;
@using Orchard.Utility.Extensions;
@using Orchard.Taxonomies.ViewModels;


@{
    if (Model.Settings.Autocomplete) {
        Style.Require("jQueryUI_Orchard");
        Script.Require("jQueryUI_Sortable");
        Script.Require("jQueryUI_Autocomplete");
        Script.Include("tagit.js").AtFoot();
        Style.Include("admin-taxonomy-tags.css");
        Script.Include("admin-taxonomy-tags.js").AtFoot();
    }

    Style.Include("admin-taxonomy.css");
    Script.Include("~/Themes/TheAdmin/scripts/admin.js").AtFoot();
    Script.Include("admin-taxonomy-expando.js").AtFoot();

    var termIndex = 0;
}
@functions {
    bool IsTermDisabled(TermEntry term) {
        return !term.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(term.Path + term.Id)));
    }
}
@{
    var allTerms = string.Join(",", Model.Terms.Select(x => string.Format("{0}\t{1}\t{2}\t{3}", x.Name, x.Id, x.GetLevels(), IsTermDisabled(x))));
    var selectedTerms = string.Join(",", Model.Terms.Where(x => x.IsChecked).Select(x => string.Format("{0}\t{1}", x.Name, x.Id)));
}
<fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)">
    <legend @if(Model.Settings.Required) { <text>class="required"</text> }>@Model.DisplayName.CamelFriendly()</legend>
    @if (Model.Settings.Autocomplete) {
    <div class="terms-editor" data-all-terms="@allTerms" data-selected-terms="@selectedTerms" data-allow-new-terms="@Model.Settings.AllowCustomTerms.ToString().ToLower()" data-singlechoice="@Model.Settings.SingleChoice.ToString().ToLower()">
        <ul></ul>
        @if (Model.Settings.SingleChoice) {
            <div class="hint">@T("Enter a single term. Hit <i>tab</i> or <i>enter</i> to apply the term.") @if (!Model.Settings.AllowCustomTerms) { <text>@T("This taxonomy does not allow you to create new terms.") </text> }</div>
        }
        else {
            <div class="hint">@T("Enter multiple terms. Hit <i>tab</i>, <i>enter</i> or <i>,</i> to add multiple terms.") @if (!Model.Settings.AllowCustomTerms) { <text>@T("This taxonomy does not allow you to create new terms.") </text> }</div>
        }
    </div>
    }
    @if (!String.IsNullOrWhiteSpace(Model.Settings.Hint)) {
    <span class="hint">@Model.Settings.Hint</span>
    }
    <div class="hidden-taxonomy-state">
        <ul class="terms">
        @foreach (var entry in Model.Terms) {
            var ti = termIndex;
            <li>
                @{
                    var disabled = IsTermDisabled(entry);
                    if (Model.Settings.SingleChoice) {
                    <input @if (disabled){ <text>disabled="disabled"</text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId){ <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name" data-term-identity="@entry.Name.ToLower()" />
                    }
                    else {
                    <input @if (disabled){ <text>disabled="disabled"</text> } type="checkbox" value="true" @if (entry.IsChecked) { <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name" data-term-identity="@entry.Name.ToLower()" /> 
                    }
                }
                <label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name</label>
                @Html.HiddenFor(m => m.Terms[ti].Id)
            </li>
            termIndex++;
        }
        </ul>
    </div>
            
    @if (!Model.Terms.Any()) {
    <div class="no-terms">
        @T("There are no terms defined for {0} yet.", Model.DisplayName.CamelFriendly())
        <a href="@Url.Action("Index", "TermAdmin", new { taxonomyId = Model.TaxonomyId, area = "Orchard.Taxonomies" })">@T("Create some terms")</a>
    </div>        
    }
    @Html.HiddenFor(m => m.TaxonomyId)
</fieldset>
